<?php
set_include_path('.'
        . PATH_SEPARATOR . './library'
        . PATH_SEPARATOR . get_include_path());

require 'Zend/Config/Ini.php';
require 'Zend/Config/Writer/Ini.php';

function __construct()
{
    $arr = array();
    $configPath = './application/configuration/config.ini';
    $config = new Zend_Config_Ini($configPath, null);
    $arr['server'] = $config->database->database->params->host;
    $arr['user'] = $config->database->database->params->username;
    $arr['pass'] = $config->database->database->params->password;
    $arr['database'] = $config->database->database->params->dbname;
    $arr['graphiz'] = $config->topologie->graphviz->location;
    $arr['tmpPath'] = $config->topologie->generation->pathTmp;
    $arr['sonde'] = $config->sonde->sonde->location;
    return $arr;
}

function writeConf()
{
    $configPath = './application/configuration/config.ini';
    $options['allowModifications'] = true;
    $writer = new Zend_Config_Writer_Ini();
    $config = new Zend_Config_Ini($configPath, null, $options);

    if (isset($_GET['server']))
        $config->database->database->params->host = $_GET['server'];
    if (isset($_GET['user']))
        $config->database->database->params->username = $_GET['user'];
    if (isset($_GET['pass']))
        $config->database->database->params->password = $_GET['pass'];
    if (isset($_GET['db']))
        $config->database->database->params->dbname = $_GET['db'];
    if (isset($_GET['tmpPath']))
    {
        $config->topologie->generation->pathTmp = $_GET['tmpPath'];
        $config->topologie->diagram->composant = $_SERVER['DOCUMENT_ROOT'] . "public/imgTmp/router.png";
    }
    if (isset($_GET['graphiz']))
        $config->topologie->graphviz->location = $_GET['graphiz'];
    if (isset($_GET['sonde']))
        $config->sonde->sonde->location = $_GET['sonde'];
    $config->topologie->generation->httpImg = "http://" . $_SERVER['HTTP_HOST'] . "/public/imgTmp/";
    $writer->write($configPath, $config);
}

$conf = __construct();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Ad-Network Web Install</title>
        <style type="text/css" >
            body {
                font-family: Verdana,Arial,sans-serif;
                font-size: 0.9em;
                margin: 0;
            }
            p {
                line-height: 17px;
                margin: 11px 0 10px;
                padding: 0;
            }
            #wrap {
                margin-left: auto;
                margin-right: auto;
                width: 730px;
            }
            #content {
                margin-top: 30px;
                width: 100%;
            }
            #top h2 {
                margin: 0;
                color: white;
                font-family: Verdana,Arial,sans-serif;
                margin-left: 15%;
                padding: 10px 0;
            }
            #clear {
                clear: both;
                display: block;
                height: 1px;
                overflow: hidden;
                width: 100%;
            }
            #footer {
                border-top: 1px dotted gray;
                margin: 40px auto 0;
                padding: 20px 0;
                text-align: center;
                width: 70%;
            }
            #footer p {
                margin: 0;
                padding: 0;
            }

            #top {
                background: #5F564D none repeat scroll 0 0; 
                width: 100%;
            }
            input[type=text] {
                -moz-border-radius:4px;
                -webkit-border-radius:4px;
                border:1px solid #C6C6C6;
                font-size:13px;
                margin:5px 0px 5px;
                padding:5px;
                width:180px;
            }
            input[type=button] {
                margin-left: 50%;
                margin-right: 50%;
            }
            fieldset {
                border: none;
            }
            .error {
                color: red;
            }
            .done {
                color: green;
            }
        </style>
        <script type="text/javascript" src="./library/JQuery/jquery.js"></script>
        <script type="text/javascript">
            //<![CDATA[
            var doneout = '<div><b><span class="done">Installation termine<br/>Install done</span></b>\n\
                    <br/><br/><span class="error">Important : Supprimer ce fichier <?php echo __FILE__; ?><br/>\n\
Important : Delete this file  <?php echo __FILE__; ?></span></div>'
            //]]>


            $('#install').before(doneout)

            function __install()
            {
                var  err = false;
                var arr = [ 'server', 'db', 'user', 'graphiz', 'tmpPath', 'sonde'  ], err = '';
                //<![CDATA[
                var errout = '<b><span class="error"><br/>Erreur vide<br/>Error empty</span></b>'
                //]]>

                for ( var i=0, l=arr.length; i< l; i++)
                {
                    if ( $('#' + arr[i]).val().length == 0  )
                    {
                        $('#' + arr[i]).after(errout);
                        err = true;
                    }
                }
                if ( err == false ) {
                    document.forms['install'].submit();
                }
            }
        </script>

    </head>
    <body>
        <div id="top">
            <h2>Ad-Network Web Install</h2>
        </div>
        <div id="wrap">
            <div id="content">
                <div id="left">
                    <form action="#" id="install">
                        <fieldset>
                            <div>
                                <p>
                                    <b>Serveur SQL</b>
                                    <br/>
                                    <b>Server SQL</b>
                                    <br/><br/>
                                    Ex : localhost || 127.0.0.1
                                    <br/>
                                </p>
                                <label for="server">Server :</label>
                                <input type="text" name="server" id="server" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['server']; ?>"/>
                            </div>
                            <hr/>
                            <div>
                                <p>
                                    <b>Nom de la base</b>
                                    <br/>
                                    <b>Name of the database</b>
                                    <br/><br/>
                                    Ex : adn_db
                                    <br/>
                                </p>
                                <label for="db">Database :</label>
                                <input type="text" name="db" id="db" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['database']; ?>"/>
                            </div>
                            <hr/>
                            <div>
                                <p>
                                    <b>Nom de l'utilisateur SQL</b>
                                    <br/>
                                    <b>Name of the SQL user</b>
                                    <br/><br/>
                                    Ex : adn_user
                                    <br/>
                                </p>
                                <label for="user">Username :</label>
                                <input type="text" name="user" id="user" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['user']; ?>"/>
                            </div>
                            <br/><br/>
                            <div>
                                <p>
                                    <b>Mot de passe de l'utilisateur SQL</b>
                                    <br/>
                                    <b>Password of the SQL user</b>
                                    <br/><br/>
                                    Ex : adn_password
                                    <br/>
                                </p>
                                <label for="pass">Password :</label>
                                <input type="text" name="pass" id="pass" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['pass']; ?>"/>
                            </div>
                            <hr/>
                            <div>
                                <p>
                                    <b>Chemin d'installation de Graphiz</b>
                                    <br/>
                                    <b>Path of the installatin of Graphiz</b>
                                    <br/><br/>
                                    Ex : C:\Programs Files\Graphiz || /usr/local/graphiz
                                    <br/>
                                </p>
                                <label for="graphiz">Graphiz :</label>
                                <input type="text" name="graphiz" id="graphiz" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['graphiz']; ?>"/>
                            </div>
                            <br/><br/>
                            <div>
                                <p>
                                    <b>Dossier temporaire pour Graphiz</b>
                                    <br/>
                                    <b>Temporary Graphiz folder</b>
                                    <br/><br/>
                                    Ex : C:\www\public\imgTmp || /var/www/public/imgTmp
                                    <br/>
                                </p>
                                <label for="tmpPath">Cache Graphiz :</label>
                                <input type="text" name="tmpPath" id="tmpPath" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['tmpPath']; ?>"/>
                            </div>
                            <hr/>
                            <div>
                                <p>
                                    <b>Chemin reseau vers la sonde</b>
                                    <br/>
                                    <b>Network path to the sonde</b>
                                    <br/><br/>
                                    Ex : http://localhost:4242 || http://127.0.0.1:4242
                                    <br/>
                                </p>
                                <label for="sonde">Sonde :</label>
                                <input type="text" name="sonde" id="sonde" class="text ui-widget-content ui-corner-all"
                                       value="<?php echo $conf['sonde']; ?>"/>
                            </div>
                        </fieldset>
                        <p>
                            <input type="button" onclick="__install()" value="Installer / Install" />
                        </p>
                    </form>
                </div>
                <div id="clear"></div>
            </div>
            <div id="footer">
                <p>Copyright &copy; 2010 <a href="http://ad-network.eu">ad-network</a> </p>
            </div>
        </div>
<?php
if (isset($_GET['server']))
{
    writeConf();
    echo '<script type="text/javascript">$("#install").before(doneout)</script>';
}
?>
    </body>
</html>
